#include "MVDataFrame.h"

CMVDataFrame::CMVDataFrame( const CMVDataFrame & frame )
{
	m_iFrameNumber = frame.m_iFrameNumber;
	m_eFrameType = frame.m_eFrameType;
	m_plstMB = new std::list<CMVDataMB*>();

	std::list<CMVDataMB*>::const_iterator i;
	for( i=frame.m_plstMB->begin(); i!=frame.m_plstMB->end(); i++ )
	{
		CMVDataMB * pMB = new CMVDataMB( *(*i) );
		m_plstMB->push_back( pMB );
	}
}

CMVDataFrame::CMVDataFrame( int framenumber, TYPE_FRAME frametype )
{
	m_iFrameNumber = framenumber;
	m_eFrameType = frametype;
	m_plstMB = new std::list<CMVDataMB*>();

}

CMVDataFrame::~CMVDataFrame()
{
	delete m_plstMB;
}

int CMVDataFrame::SetFrameNumber( int framenumber )
{
	m_iFrameNumber = framenumber;
	return 0;
}

int CMVDataFrame::GetFrameNumber() const
{
	return m_iFrameNumber;
}

int CMVDataFrame::SetFrameType( TYPE_FRAME frametype )
{
	m_eFrameType = frametype;
	return 0;
}

TYPE_FRAME CMVDataFrame::GetFrameType() const
{
	return m_eFrameType;
}

int CMVDataFrame::Clear()
{
	m_plstMB->clear();
	return 0;
}

const CMVDataMB * CMVDataFrame::GetMBByCoodinate( int x, int y ) const
{
	std::list<CMVDataMB*>::const_iterator i;	
	for( i=m_plstMB->begin(); i!=m_plstMB->end(); i++ )
	{
		if( (*i)->GetCoordinateX() == x && (*i)->GetCoordinateY() == y )
		{
			return *i;
		}
	}

	return NULL;
}

const CMVDataMB * CMVDataFrame::GetNextMB( const CMVDataMB * curr_mb ) const
{
	if( m_plstMB->empty() )
	{
		return NULL;
	}
	if( NULL == curr_mb )
	{
		return *(m_plstMB->begin());
	}

	for( std::list<CMVDataMB*>::iterator i = m_plstMB->begin(); i!=m_plstMB->end(); i++ )
	{
		if( curr_mb == *i )
		{
			i++;
			if( i!=m_plstMB->end() )
			{
				return *i;
			}
			break;
		}
	}

	return NULL;
}


int CMVDataFrame::AddMB( const CMVDataMB * mbdata )
{
	
	std::list<CMVDataMB*>::iterator i=m_plstMB->begin();

	CMVDataMB * pNewMB = new CMVDataMB( *mbdata );

	// Find the insert point
	while ( i != m_plstMB->end() && (*pNewMB) != (*(*i)) )
	{
		i++;
	}

	if( i == m_plstMB->end() )
	{
		// nothing found, reach the end of the list
		m_plstMB->push_back( pNewMB );
	}
	else
	{
		// there is already an item with the same frame number, update it
		delete *i;
		m_plstMB->remove( *i );
		m_plstMB->insert( i, pNewMB );
		
	}

	return 0;			
}

int CMVDataFrame::RemoveMB( int x, int y )
{
	
	std::list<CMVDataMB*>::iterator i=m_plstMB->begin();

	// Find the insert point
	for ( ; i != m_plstMB->end(); i++ )
	{
		if ( (*i)->GetCoordinateX() == x && (*i)->GetCoordinateY() == y )
		{
			delete *i;
			m_plstMB->remove( *i );
			return 0;
		}
	}

	return -1;
}

bool CMVDataFrame::operator == ( const CMVDataFrame& framedata ) const
{
	if( this->m_iFrameNumber == framedata.m_iFrameNumber )
	{
		return true;
	}
	return false;
}

bool CMVDataFrame::operator != ( const CMVDataFrame& framedata ) const
{
	if( this->m_iFrameNumber == framedata.m_iFrameNumber )
	{
		return false;
	}
	return true;
}

CMVDataFrame& CMVDataFrame::operator = ( const CMVDataFrame& framedata )
{
	m_iFrameNumber = framedata.m_iFrameNumber;
	m_eFrameType = framedata.m_eFrameType;

	m_plstMB->clear();
	m_plstMB->assign( framedata.m_plstMB->begin(), framedata.m_plstMB->end() );

	return *this;
}

int CMVDataFrame::GetMBCount() const
{
	return static_cast<int>( m_plstMB->size() );
}
